/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function (nums, target) {
  let left = 0
  let right = nums.length - 1
  let moveDir = left
  if (nums[left] >= target) {
    moveDir = left
    while (moveDir <= right) {
      if (nums[moveDir] === target) {
        return moveDir
      }
      moveDir++
    }
  } else {
    moveDir = right
    while (moveDir >= left) {
      if (nums[moveDir] === target) {
        return moveDir
      }
      moveDir--
    }
  }
  return -1
}

console.log(search([4, 5, 6, 7, 0, 1, 2], 0))
